package org.rency.spring.cloud.boot.mvc.service;

import org.rency.spring.cloud.boot.mvc.customer.MemberDetail;
import org.rency.spring.cloud.boot.mvc.domain.MemberDomain;
import org.rency.spring.cloud.boot.mvc.repository.MemberRepository;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * $TODO
 *
 * @author: Created by user_rcy@163.com on 2016/08/17.
 * @version: V1.0.0
 * @since JDK 1.8
 */
@Service("memberService")
public class MemberServiceImpl implements MemberService{

    @Resource(name="memberRepository")
    private MemberRepository memberRepository;

    @Override
    public MemberDomain add(MemberDomain member) {
        if(memberRepository.add(member)){
            return member;
        }
        return null;
    }

    @Override
    public boolean remove(String memberId) {
        return memberRepository.remove(memberId);
    }

    @Override
    public MemberDomain get(String memberId) {
        return memberRepository.get(memberId);
    }

    @Override
    public List<MemberDomain> loadAll() {
        return memberRepository.loadAll();
    }

    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        MemberDomain member = memberRepository.getLogin(username);
        if(member == null){
            throw new UsernameNotFoundException(username+" not exists!");
        }
        MemberDetail detail = new MemberDetail();
        detail.setUsername(member.getMemberName());
        detail.setPassword(member.getPassword());
        return detail;
    }
}
